package com.hykj.xxltest.config;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.hykj.xxltest.base.RequiresRole;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Set;

/**
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket userApi() {

        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler requestHandler) {
                if (requestHandler.isAnnotatedWith(RequiresRole.class)) {//只有添加了ApiOperation注解的method才在API中显示

                    Optional<RequiresRole> ob = requestHandler.findAnnotation(RequiresRole.class);
                    RequiresRole requiresRole = ob.get();
                    if (StringUtils.isEmpty(requiresRole.role())) {
                        return true;
                    }
                    Set<Integer> set = RequireRoleAspect.getRequireRoles(requiresRole);
                    if (!set.contains(0)) {
                        System.out.println("xxxxx " + requestHandler.getName());
                        return false;
                    }
                    return true;
                }

                if (requestHandler.isAnnotatedWith(ApiOperation.class)) {//只有添加了ApiOperation注解的method才在API中显示
                    return true;
                }
                return false;
            }


        };

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("给前端的接口")
                .forCodeGeneration(true)
                .pathMapping("/")
                .select()
                .apis(predicate)
                .paths(PathSelectors.any())
                .build()
                .apiInfo(allApiInfo());

    }


    @Bean
    public Docket allApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("所有接口")
                .forCodeGeneration(true)
                .pathMapping("/")// base，最终调用接口后会和paths拼接在一起
                .select()
                .paths(PathSelectors.any())
                .build()
                .apiInfo(allApiInfo());

    }


    private ApiInfo allApiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")//大标题
//                .description("全部的接口")//详细描述
//                .version("1.0")//版本
//                .termsOfServiceUrl("NO terms of service")
//                .license("The Apache License, Version 2.0")
//                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }

}